package com.queen.restaurant.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.queen.restaurant.domain.Course;
import com.queen.restaurant.domain.CourseType;
import com.queen.restaurant.service.CourseService;
import com.queen.restaurant.service.CourseTypeService;

/**
 * 菜品操作Controller
 * 
 * @author Queen
 * Description:
 * <br/>Program Name:QueenRestaurant
 * <br/>Date:2013.07.11
 * */

@Controller
public class CourseController {

	@Autowired
	private CourseService courseService;
	
	@Autowired
	private CourseTypeService courseTypeService;
	
	// 获取菜品页面
	@RequestMapping(value = "/restaurant/course")
	public String getCourseTypePage() {
		return "restaurant/restaurant_course";
	}

	// 获取菜品类别下的所有菜品
	@RequestMapping(value = "/restaurant/course/list")
	@ResponseBody
	public List<Course> getCourseList(@RequestBody String courseTypeId) {
		return courseService.getCourseByCourseTypeId(Integer.parseInt(courseTypeId));
	}
	
	// 新增菜品
	@RequestMapping(value = "/restaurant/course/create", method = RequestMethod.POST)
	@ResponseBody
	public List<Course> createCourse(
			@RequestBody Course course,
			@RequestBody String courseTypeId){
		CourseType courseType = courseTypeService
				.getCourseTypeByCourseTypeId(Integer.parseInt(courseTypeId));
//		course.setCourseType(courseType);
		courseType.getCourseList().add(course);
		courseService.createCourse(course);
		return courseService.getCourseByCourseTypeId(Integer.parseInt(courseTypeId));
	}
	
	// 删除菜品
	@RequestMapping(value = "/restaurant/course/delete", method = RequestMethod.POST)
	@ResponseBody
	public List<Course> deleteCourse(
			@RequestBody String courseTypeId,
			@RequestBody String courseId){
		courseService.deleteCourseById(Integer.parseInt(courseId));
		return courseService.getCourseByCourseTypeId(Integer.parseInt(courseTypeId));
	}
	
	// 修改菜品
	@RequestMapping(value = "/restaurant/course/update", method = RequestMethod.POST)
	@ResponseBody
	public List<Course> updateCourse(
			@RequestBody Course course,
			@RequestBody String courseTypeId){
		
		courseService.updateCourse(course);
		return courseService.getCourseByCourseTypeId(Integer.parseInt(courseTypeId));
	}	
}
